package com.briup;

import java.util.concurrent.TimeUnit;

/**
 * 三种阻塞方式：sleep、yield、join
 *
 * @auther thb
 * @date 2021/9/3
 */
public class T3_sleep_yield_join {

    public static void main(String[] args) throws InterruptedException {
        Thread2 t1 = new Thread2();
        Thread2 t2 = new Thread2();
        Thread2 t3 = new Thread2();

        System.out.println("main线程阻塞：");
        System.out.println("t1的状态："+t1.getState());

        t1.start();
        t2.start();
//        t1.join();
        t3.start();
        System.out.println("t1的状态："+t1.getState());
        for (int i = 0; i < 4; i++) {
            TimeUnit.SECONDS.sleep(1);
            System.out.println(Thread.currentThread().getName() + "：" + i);
            System.out.println("t1的状态："+t1.getState());
        }
        System.out.println("t1的状态："+t1.getState());
    }

}

class Thread2 extends Thread {

    @Override
    public void run() {
        for (int i = 0; i < 4; i++) {
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
//            Thread.yield();
            System.out.println(Thread.currentThread().getName() + "：" + i);
        }
    }

}